package xml;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ReadXml {

	public static void main(String[] args) {
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read(new File("subject.xml"));
			Element root = doc.getRootElement();
			System.out.println("根元素是：" + root.getName());
			List<Element> list = root.selectNodes("subject");
			for (Element e : list) {
				String type = e.attributeValue("type");
				String unit = e.attributeValue("unit");
				// List<Element> l2 = e.selectNodes("name");
				// String name = l2.get(0).getText();
				String name = e.selectSingleNode("name").getText();
				Element etime = (Element) e.selectSingleNode("times");
				int times = Integer.parseInt(etime.getText());

				System.out.println(unit + ",类型：" + type + ",课程名:" + name
						+ ",课时：" + times);
				System.out.println(">>>>>>>>>>>>>>");

			}
		} catch (DocumentException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}

}
